package com.powerunion.datacollection.service;

import java.util.Date;
import net.sf.json.JSONObject;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.quartz.JobDetail;

import com.powerunion.datacollection.common.Constants;
import com.powerunion.datacollection.common.Format;
import com.powerunion.datacollection.common.SystemException;
import com.powerunion.datacollection.dao.DaoFactory;
import com.powerunion.datacollection.dao.ItemDao;
import com.powerunion.datacollection.model.ItemCategory;
import com.powerunion.datacollection.model.ItemInfo;
import com.powerunion.datacollection.model.jsonObject.ItemInfoJSONObject;
import com.powerunion.datacollection.model.query.ItemInfoQuery;
import com.powerunion.datacollection.quartz.TaskScheduler;
import com.powerunion.datacollection.quartz.job.CollectionTask;
import com.powerunion.datacollection.quartz.job.UrgencyTask;
import com.powerunion.util.log.LogHelper;

public class ItemManager {
	private static Logger logger = LogHelper.getInstance().getLogger(
			ItemManager.class);
	private ItemDao dao = null;

	public ItemManager() {
		super();
		this.dao = (ItemDao) DaoFactory.getInstance().getDao(
				ItemDao.class);
	}

	public String save(String saveStr, String currentUser,int itemType)  throws Exception {
		// 参数为空
		if (StringUtils.isEmpty(saveStr)) {
			String[] args = { "saveStr" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter saveStr NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		JSONObject jsonObject = JSONObject.fromObject(saveStr);
		ItemInfoJSONObject itijo = (ItemInfoJSONObject) JSONObject.toBean(
				jsonObject, ItemInfoJSONObject.class);
		
		ItemInfo itemInfo = new ItemInfo();

		//复制属性值
	    BeanUtils.copyProperties(itemInfo,itijo);

	    String createDateStr = itijo.getCrtDateStr();
	    Date createDate = Format.getInstance().stringToDate(createDateStr, "yyyy-MM-dd");
	    itemInfo.setCrtDate(createDate);
	    
	    if(itemInfo.getId().equals("")) {
	    	itemInfo.setCrtUser(currentUser);
	    }else if(itemType == Constants.PROINSTANCE){
	    	String[]codes = new String[1];
	    	codes[0] = itemInfo.getId();
	    	//先删除环节信息
	    	ProcessInstanceManager pm = new ProcessInstanceManager();
	    	pm.delete(itemInfo.getId());
	    	//接着删除流程实例定义信息
	    	this.dao.delete(codes);
	    	itemInfo.setId("");
	    }
	    itemInfo.setModiUser(currentUser);
	    itemInfo.setModiDate(new Date());
	    itemInfo.setItemType(itemType);
	    
		return this.dao.save(itemInfo);

	}
	
	public String save(ItemInfo itemInfo) throws Exception{
		// 参数为空
		if (itemInfo == null) {
			String[] args = { "itemInfo" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter itemInfo NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
    	String[]codes = new String[1];
    	codes[0] = itemInfo.getId();
		this.dao.delete(codes);
		return this.dao.save(itemInfo);
	}

	public boolean delete(String[] codes) throws Exception{
		// 参数为空
		if (codes.length < 1) {
			String[] args = { "codes" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter codes NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}

		return this.dao.delete(codes);
	}

	public ItemInfo findByKey(String id) throws Exception{
		// 参数为空
		if (StringUtils.isEmpty(id)) {
			String[] args = { "id" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter id NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}

		return this.dao.getBykey(id);
	}
	
	public ItemInfo findByKey(String id,int itemType) throws Exception{
		// 参数为空
		if (StringUtils.isEmpty(id)) {
			String[] args = { "id" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter id NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}

		return this.dao.getBykey(id,itemType);
	}

	public String getItemInfos(String queryStr,
			int start, int limit)throws Exception {
		// 参数为空
		if (StringUtils.isEmpty(queryStr)) {
			String[] args = { "queryStr" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter queryStr NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
	
		JSONObject jsonObject = JSONObject.fromObject(queryStr);
		ItemInfoQuery query = (ItemInfoQuery)JSONObject.toBean(jsonObject,ItemInfoQuery.class);
		 
		String result = this.dao.findItemInfos(query, start, limit);
		return result;
	}

	
}
